/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain (a) copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sentry.ohos.core;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;

import java.io.Closeable;
import java.nio.file.Paths;

import io.sentry.IHub;
import io.sentry.ILogger;
import io.sentry.Integration;
import io.sentry.OutboxSender;
import io.sentry.SentryLevel;
import io.sentry.SentryOptions;
import io.sentry.util.Objects;

/**
 * Watches the envelope dir. and send them (events) over.
 *
 * @since 2021-06-01
 */
public abstract class EnvelopeFileObserverIntegration implements Integration, Closeable {
    private @Nullable EnvelopeFileObserver observer;
    private @Nullable ILogger logger;

    /**
     * getOutboxFileObserver
     *
     * @return EnvelopeFileObserverIntegration
     */
    public static @NotNull EnvelopeFileObserverIntegration getOutboxFileObserver() {
        return new OutboxEnvelopeFileObserverIntegration();
    }

    @Override
    public final void register(final @NotNull IHub hub, final @NotNull SentryOptions options) {
        Objects.requireNonNull(hub, "Hub is required");
        Objects.requireNonNull(options, "SentryOptions is required");

        logger = options.getLogger();

        final String path = getPath(options);
        if (path == null) {
            logger.log(
                SentryLevel.WARNING,
                "Null given as (a) path to EnvelopeFileObserverIntegration. Nothing will be registered.");
        } else {
            logger.log(
                SentryLevel.DEBUG, "Registering EnvelopeFileObserverIntegration for path: %s", path);

            final OutboxSender outboxSender =
                new OutboxSender(
                    hub,
                    options.getEnvelopeReader(),
                    options.getSerializer(),
                    logger,
                    options.getFlushTimeoutMillis());
            observer =
                new EnvelopeFileObserver(Paths.get(path), outboxSender, logger, options.getFlushTimeoutMillis());
            logger.log(SentryLevel.DEBUG, "EnvelopeFileObserverIntegration installed.");
        }
    }

    @Override
    public void close() {
        if (observer != null) {
            if (logger != null) {
                logger.log(SentryLevel.DEBUG, "EnvelopeFileObserverIntegration removed.");
            }
        }
    }

    /**
     * getPath
     *
     * @param options SentryOptions
     * @return String
     */
    @TestOnly
    public abstract @Nullable String getPath(final @NotNull SentryOptions options);

    /**
     * OutboxEnvelopeFileObserverIntegration
     *
     * @since 2021-06-01
     */
    private static final class OutboxEnvelopeFileObserverIntegration
        extends EnvelopeFileObserverIntegration {
        @Override
        @Nullable
        public String getPath(final @NotNull SentryOptions options) {
            return options.getOutboxPath();
        }
    }
}
